package com.ecube.mqtt.page.listener;

import com.ecube.mqtt.page.listener.chargelistener.BackListener;
import com.ecube.mqtt.page.listener.swapListener.SwapRateListener;
import com.ecube.mqtt.page.listener.swapListener.SwapStartListener;
import com.ecube.mqtt.page.listener.swapListener.SwapStartResultPageListener;
import com.ecube.mqtt.page.listener.swapListener.SwapTCUStartListener;
import com.ecube.mqtt.page.listener.vehicle.BBOXSendListener;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class VehicleListener implements ActionListener {
    private JButton chargeButton;
    private String path;
    private JFrame first;

    public VehicleListener(JFrame first, JButton chargeButton, String path) {//获取登录界面、按钮对象
        this.first = first;
        this.path = path;
        this.chargeButton = chargeButton;
    }


    public void actionPerformed(ActionEvent e) {
        first.dispose();
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);
        Font font = new Font("宋体",Font.PLAIN,18);

        //生成新界面
        JFrame vehicle = new JFrame();
        vehicle.setSize(500, 850);
        vehicle.setTitle("车辆");//设置窗体标题
        vehicle.setDefaultCloseOperation(2);
        vehicle.setLocationRelativeTo(null);
        vehicle.setFont(font);  //宋体，正常风格，14号字体
        //实例化FlowLayout流式布局类的对象，指定对齐方式为居中对齐组件之间的间隔为10个像素
        FlowLayout fl = new FlowLayout(FlowLayout.RIGHT,30,30);
        //实例化流式布局类的对象
        vehicle.setLayout(fl);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel labname = new JLabel("环境：");
        labname.setFont(font);
        //将labname标签添加到窗体上
        vehicle.add(labname);
        String envfirst = "";
        try {
            envfirst = PropertyUtil.getProperties("env", CommonConstant.SWAP_CONFIG_PATH);
           // path + "config\\swap.properties"
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        JComboBox env = new JComboBox();
        env.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        env.setFont(font);
        env.addItem("DEV");
        env.addItem("FAT");
        env.addItem("UAT");
        env.addItem("HG");
        env.setSelectedItem(envfirst);
        //env.getSelectedItem().toString()
        vehicle.add(env);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel batteryname = new JLabel("电池ID：");
        batteryname.setFont(font);
        //将labname标签添加到窗体上
        vehicle.add(batteryname);

        String batteryfirst = "0PFPBF16GC01";
        //实例化JTextField标签对象化
        JTextField batteryIdtext = new JTextField(batteryfirst);
        batteryIdtext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        batteryIdtext.setFont(font);
        //将textName标签添加到窗体上
        vehicle.add(batteryIdtext);


        //实例化JLabel标签对象，该对象显示“vin”
        JLabel vin = new JLabel("vin：");
        vin.setFont(font);
        vehicle.add(vin);
        String vinfirst = "LA91H98D0R1BRE144";
        //实例化JTextField标签对象化
        JTextField vintext = new JTextField(vinfirst);
        vintext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        vintext.setFont(font);
        vehicle.add(vintext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel totalMileage = new JLabel("总里程：");
        totalMileage.setFont(font);
        vehicle.add(totalMileage);

        //实例化JTextField标签对象化
        JTextField totalMileagetext = new JTextField("12982");
        totalMileagetext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        totalMileagetext.setFont(font);
        vehicle.add(totalMileagetext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel todayMileage = new JLabel("今日里程：");
        todayMileage.setFont(font);
        vehicle.add(todayMileage);

        //实例化JTextField标签对象化
        JTextField todayMileagetext = new JTextField("358");
        todayMileagetext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        todayMileagetext.setFont(font);
        vehicle.add(todayMileagetext);


        //实例化JLabel标签对象，该对象显示“账号”
        JLabel soc = new JLabel("        soc：");
        soc.setFont(font);
        vehicle.add(soc);

        //实例化JTextField标签对象化
        JTextField soctext = new JTextField("87");
        soctext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        soctext.setFont(font);
        vehicle.add(soctext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel soh = new JLabel("        soh：");
        soh.setFont(font);
        vehicle.add(soh);

        //实例化JTextField标签对象化
        JTextField sohtext = new JTextField("95");
        sohtext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        sohtext.setFont(font);
        vehicle.add(sohtext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel lon = new JLabel("       lon：");
        lon.setFont(font);
        vehicle.add(lon);

        //实例化JTextField标签对象化
        JTextField lontext = new JTextField("99.880951");
        lontext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        lontext.setFont(font);
        vehicle.add(lontext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel lat = new JLabel("       lat：");
        lat.setFont(font);
        vehicle.add(lat);

        //实例化JTextField标签对象化
        JTextField lattext = new JTextField("31.63566");
        lattext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        lattext.setFont(font);
        vehicle.add(lattext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel BBOXiccID = new JLabel(" BBOXiccID：");
        BBOXiccID.setFont(font);
        vehicle.add(BBOXiccID);

        String BBOXiccIDfirst = "";
        try {
            BBOXiccIDfirst = PropertyUtil.getProperties("BBOXiccIDtext", CommonConstant.VEHICLE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField BBOXiccIDtext = new JTextField(BBOXiccIDfirst);
        BBOXiccIDtext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        BBOXiccIDtext.setFont(font);
        vehicle.add(BBOXiccIDtext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel TBOXiccID = new JLabel(" TBOXiccID：");
        TBOXiccID.setFont(font);
        vehicle.add(TBOXiccID);
        String TBOXiccIDfirst = "";
        try {
            TBOXiccIDfirst = PropertyUtil.getProperties("TBOXiccIDtext", CommonConstant.VEHICLE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField TBOXiccIDtext = new JTextField(TBOXiccIDfirst);
        TBOXiccIDtext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        TBOXiccIDtext.setFont(font);
        vehicle.add(TBOXiccIDtext);



        //实例化JButton组件
        JButton bboxSendbutton = new JButton();
        //设置按键的显示内容
        bboxSendbutton.setText("     发送BBOX    ");
        bboxSendbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        bboxSendbutton.setSize(dim3);
        vehicle.add(bboxSendbutton);

        BBOXSendListener bboxSend = new BBOXSendListener(vehicle,
                env,
                batteryIdtext,
                vintext,
                totalMileagetext,
                todayMileagetext,
                soctext,
                sohtext,
                lontext,
                lattext,
                BBOXiccIDtext,
                TBOXiccIDtext);
        bboxSendbutton.addActionListener(bboxSend);



        //实例化JButton组件
        JButton vehicleReturnFirstPageButton = new JButton();
        //设置按键的显示内容
        vehicleReturnFirstPageButton.setText("                 返回首页                ");
        vehicleReturnFirstPageButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        vehicleReturnFirstPageButton.setSize(dim3);
        vehicle.add(vehicleReturnFirstPageButton);



        BackListener vehicleBack = new BackListener(vehicle, path);
        vehicleReturnFirstPageButton.addActionListener(vehicleBack);

        vehicle.setResizable(false);
        vehicle.setVisible(true);
    }
}